import java.util.Comparator;
import java.util.PriorityQueue;


class IntCmp implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

public class Work {
    public static int[] findMinNums(int[] array,int k){
        if(k <= 0){
            return new int[0];
        }
        IntCmp intCmp = new IntCmp();
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(k,intCmp);
        for (int i = 0; i < k; i++) {
            minHeap.offer(array[i]);
        }

        for (int i = k; i < array.length; i++) {
            int top = minHeap.peek();
            if(array[i] < top){
                minHeap.poll();
                minHeap.offer(array[i]);
            }
        }
        int[] minKNums = new int[k];
        for (int i = 0; i < k; i++) {
            minKNums[i] = minHeap.poll();
        }

        return minKNums;
    }



    class Solution {
        public int[] smallestK(int[] arr, int k) {
            if(k <= 0){
                return new int[0];
            }
            IntCmp intCmp = new IntCmp();
            PriorityQueue<Integer> q = new PriorityQueue<>(k,intCmp);
            for(int i = 0; i < arr.length; ++i){
                // 先使arr中前k个元素创建小堆
                if(i < k){
                    q.offer(arr[i]);
                }else{
                    // 使用堆顶与arr中剩余元素进行比较，如果arr[i]小于堆顶元素时，则替换堆顶元素
                    if(intCmp.compare(arr[i], q.peek()) > 0){
                        q.poll();
                        q.offer(arr[i]);
                    }
                }
            }
            // 堆中剩余的n个元素就是所要的k个元素
            int[] ret = new int[k];
            for(int i = 0; i < k; ++i){
                ret[i] = q.poll();
            }
            return ret;
        }
    }

}
